package com.example.demo.controller;

import com.example.demo.mapper.ProductMapper;
import com.example.demo.model.ProductModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * Create by keboom on 2020/6/24
 */
@Controller
public class AdminProductController {

    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/admin/product")
    public String productList(Model model){
        List<ProductModel> productModels = productMapper.selectAll();
        System.out.println(productModels);
        model.addAttribute("products",productModels);
        return "product/prolist";
    }

    @GetMapping("/admin/productAdd")
    public String getproductAdd(){
        return "product/proadd";
    }

    //添加商品的接口
    @PostMapping("/admin/productAdd")
    public String productAdd(String productPicture,String productTitle,String productContent,Integer productPrice,Model model){
        ProductModel productModel = new ProductModel();
        productModel.setProductPicture(productPicture);
        productModel.setProductTitle(productTitle);
        productModel.setProductContent(productContent);
        productModel.setProductPrice(productPrice);
        productMapper.insertPoduct(productModel);
        List<ProductModel> productModels = productMapper.selectAll();
        model.addAttribute("products",productModels);
        return "product/prolist";
    }
}
